package com.itheima.health.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper,CheckGroup> implements CheckGroupService {
    @Override
    public void add(CheckGroupDTO checkGroupDTO) {
        Integer id = checkGroupDTO.getId();
        if (id!=null) {
            baseMapper.relieveByGroupId(id);
        }
        saveOrUpdate(checkGroupDTO);
        id = checkGroupDTO.getId();
        System.out.println(id);

        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        System.out.println(checkitemIds);
        if (checkitemIds!=null&&checkitemIds.length!=0) {
            for (Integer checkitemId : checkitemIds) {
                baseMapper.addCheckGroupAndCheckItemIds(id,checkitemId);
            }
        }
    }

//    @Override
//    public List<CheckItem> findAll() {
//        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper();
//        // 有效数据
//        queryWrapper.eq("is_delete",0);
//        List<CheckItem> list = list(queryWrapper);
//        return list;
//    }
//
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<CheckGroup> page = null;
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);
        if (StringUtils.isEmpty(queryPageBean.getQueryString())) {
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        } else {
            queryWrapper.and(
                    wrapper ->
                            wrapper.like("name", queryPageBean.getQueryString())
                                    .or()
                                    .like("code",queryPageBean.getQueryString())
                                    .or()
                                    .like("helpcode",queryPageBean.getQueryString())
            );
//            queryWrapper.like("name",queryPageBean.getQueryString());
//            queryWrapper.or();
//            queryWrapper.like("code",queryPageBean.getQueryString());

            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public List<Integer> findCheckItemIdsByGroupId(int id) {
        List<Integer> list = baseMapper.findCheckItemIdsByGroupId(id);
        return list;
    }

    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);
        List<CheckGroup> list = list(queryWrapper);
        return list;
    }

    @Override
    public List<CheckItem> findCheckItemsByGid(Integer id) {
        return baseMapper.findCheckItemsByGid(id);
    }
}
